package com.common.sdk.net.connect.http;

import android.content.Context;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Cache;
import okhttp3.Interceptor;

/**
 * Created by chengjiangsang116630 on 2018/2/5.
 */

public class SohuNetConfig {

    // 可配置的项目包括：
    // 线程池大小、线程池队列大小、基础参数、缓存等，之后要配置的东西都放在此类里面

    private static final int DEFAULT_POOL_SIZE = 2;
    private static final int DEFAULT_QUEUE_SIZE = 30;

    final int poolSize;
    final int poolQueueSize;
    final Cache cache;
    final Map<String, Object> baseParams;
    final List<Interceptor> interceptors;

    private SohuNetConfig(SohuNetConfig.Builder builder) {
        this.poolSize = builder.poolSize;
        this.poolQueueSize = builder.poolQueueSize;
        this.cache = builder.cache;
        this.baseParams = builder.baseParams;
        this.interceptors = builder.interceptors;
    }

    public SohuNetConfig.Builder newBuilder() {
        return new SohuNetConfig.Builder(this);
    }

    @Override public String toString() {
        return "SohuNetConfig{poolSize="
                + poolSize
                + ", poolQueueSize="
                + poolQueueSize
                + ", cache="
                + cache
                + ", baseParam="
                + baseParams
                + '}';
    }

    public int getPoolSize() {
        return poolSize;
    }

    public int getPoolQueueSize() {
        return poolQueueSize;
    }

    public Cache getCache() {
        return cache;
    }

    public Map<String, Object> getBaseParams() {
        return baseParams;
    }

    public List<Interceptor> getInterceptors() {
        return interceptors;
    }

    public static class Builder {

        int poolSize;
        int poolQueueSize;
        Cache cache;
        Map<String, Object> baseParams;
        List<Interceptor> interceptors;

        public Builder(Context context) {
            this.poolSize = DEFAULT_POOL_SIZE;
            this.poolQueueSize = DEFAULT_QUEUE_SIZE;
            baseParams = new HashMap<>();
            //缓存文件夹
            File cacheDir = context.getExternalFilesDir(null);
            File dataCahceDir = new File(cacheDir, "DATACACHE");
            //缓存大小为100M
            int cacheSize = 100 * 1024 * 1024;
            //创建缓存对象
            cache = new Cache(dataCahceDir,cacheSize);
        }

        Builder(SohuNetConfig config) {
            this.poolSize = config.poolSize;
            this.poolQueueSize = config.poolQueueSize;
            this.cache = config.cache;
            this.baseParams = config.baseParams;
            this.interceptors = config.interceptors;
        }

        /**
         * 只能设置一次，后期更改无效
         * @param poolSize
         * @return
         */
        public SohuNetConfig.Builder setPoolSize(int poolSize) {
            if (poolSize <= 0) {
                return this;
            }
            this.poolSize = poolSize;
            return this;
        }

        /**
         * 只能设置一次，后期更改无效
         * @param queueSize
         * @return
         */
        public SohuNetConfig.Builder setPoolQueueSize(int queueSize) {
            if (queueSize <= 0) {
                return this;
            }
            this.poolQueueSize = queueSize;
            return this;
        }

        public SohuNetConfig.Builder setBaseParams(Map<String, Object> baseParams) {
            this.baseParams = baseParams;
            return this;
        }

        public SohuNetConfig.Builder addBaseParam(String name, Object value) {
            if (value == null) {
                return this;
            }
            baseParams.put(name, value);
            return this;
        }

        public SohuNetConfig.Builder removeBaseParam(String name, Object value) {
            if (value == null) {
                return this;
            }
            baseParams.remove(name);
            return this;
        }

        public SohuNetConfig.Builder setInterceptors(List<Interceptor> interceptors) {
            this.interceptors = interceptors;
            return this;
        }

        /**
         * 只能设置一次，后期更改无效
         * @param cache
         * @return
         */
        public SohuNetConfig.Builder cache(Cache cache) {
            this.cache = cache;
            return this;
        }

        public SohuNetConfig build() {
            return new SohuNetConfig(this);
        }
    }

}
